package com.skynet.supervision.aop.dict;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.skynet.supervision.beans.response.SysDictDataVo;
import com.skynet.supervision.utils.CollectionExtUtils;
import com.skynet.supervision.utils.DictUtils;
import com.skynet.supervision.utils.Strings;
import lombok.val;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

public class DictDataSerializer extends JsonSerializer<String> implements ContextualSerializer {
    private Dict dict;
    private String fieldName;

    public DictDataSerializer(Dict dict, String fieldName) {
        this.dict = dict;
        this.fieldName = fieldName;
    }

    public DictDataSerializer() {
    }

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializerProvider) throws IOException {
        if (dict != null) {
            String key = dict.value();
            gen.writeString(value);
            gen.writeStringField(Strings.sprintf("{}DictKey", fieldName), key);
            //从字典匹配
            List<SysDictDataVo> dictData = DictUtils.getDictDataMulti(key, value);
            String dictName = "";
            if(CollectionExtUtils.notEmpty(dictData)){
                dictName = dictData.get(0).getDictName();
            }
            val dictValues = Strings.join(dictData.stream().map(SysDictDataVo::getValue).collect(Collectors.toList()), DictUtils.SEPARATOR);
            val dictSorts = Strings.join(dictData.stream().map(SysDictDataVo::getSort).collect(Collectors.toList()), DictUtils.SEPARATOR);
            gen.writeStringField(Strings.sprintf("{}DictName", fieldName), dictName);
            gen.writeStringField(Strings.sprintf("{}DictValue", fieldName), dictValues);
            gen.writeStringField(Strings.sprintf("{}DictSort", fieldName), dictSorts);
//            SysDictDataVo dictData = DictUtils.getDictCache(key, value);
//            if (CollectionExtUtils.notEmpty(dictData)) {
//            gen.writeStringField(Strings.sprintf("{}DictName", fieldName),dictNames);
//            gen.writeStringField(Strings.sprintf("{}DictValue", fieldName), dictValues);
//            gen.writeStringField(Strings.sprintf("{}DictSort", fieldName), dictSorts);
//            } else {
//                String dictValue = DictUtils.getDictValue(key, value);
//                gen.writeStringField(Strings.sprintf("{}DictName", fieldName), "");
//                gen.writeStringField(Strings.sprintf("{}DictValue", fieldName), "");
//                gen.writeStringField(Strings.sprintf("{}DictSort", fieldName), "");
//            }
        } else {
            gen.writeString(value);
        }
    }

    @Override
    public JsonSerializer<?> createContextual(SerializerProvider serializerProvider, BeanProperty beanProperty) throws JsonMappingException {
        Dict annotation = beanProperty.getAnnotation(Dict.class);
        if (annotation != null) {
            return new DictDataSerializer(annotation, findJsonFieldName(beanProperty));
        }
        return this;
    }

    private String findJsonFieldName(BeanProperty property) {
        JsonProperty jsonProperty = property.getAnnotation(JsonProperty.class);
        if (jsonProperty != null && StringUtils.isNotBlank(jsonProperty.value())) {
            return jsonProperty.value();
        }
        return property.getName();
    }
}
